package com.swpu.cbg.cnshop.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.internal.widget.TintTypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.swpu.cbg.cnshop.R;

/**
 * Created by chenboge on 16/2/10.
 */
public class NumAddSubView extends LinearLayout implements View.OnClickListener {

    private LayoutInflater mInflater;
    private Button AddButton;
    private TextView NumText;
    private Button SubButton;
    private View mView;

    private int value;
    private int maxvalue;
    private int minvalue;

    private OnAddSubClickListener mListener;

    public void setOnAddSubClickListener(OnAddSubClickListener listener) {
        this.mListener = listener;
    }

    public int getValue() {
        String val = NumText.getText().toString();
        if (val != null && !val.equals("")) {
            this.value = Integer.parseInt(val);
        }
        return value;
    }

    public void setValue(int value) {
        NumText.setText(value+"");
        this.value=value;
    }

    public int getMaxvalue() {
        return maxvalue;
    }

    public void setMaxvalue(int maxvalue) {
        this.maxvalue = maxvalue;
    }

    public int getMinvalue() {
        return minvalue;
    }

    public void setMinvalue(int minvalue) {
        this.minvalue = minvalue;
    }

    public NumAddSubView(Context context) {
        this(context, null);
    }

    public NumAddSubView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public void Add() {
        if (value < maxvalue) {
            value++;
        }
        setValue(value);
    }

    public void sub() {
        if (value > minvalue) {
            value--;
        }
        setValue(value);
    }


    public NumAddSubView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mInflater = LayoutInflater.from(context);
        initView();

        TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs, R.styleable.NumAddSubView,defStyleAttr,0);

        int value = a.getInt(R.styleable.NumAddSubView_value, 1);
        setValue(value);


        int maxvalue = a.getInt(R.styleable.NumAddSubView_maxvalue, 10);
        setMaxvalue(maxvalue);


        int minvalue = a.getInt(R.styleable.NumAddSubView_minvalue, 1);
        setMinvalue(minvalue);


        Drawable leftBack = a.getDrawable(R.styleable.NumAddSubView_leftbutonBackground);
        setAddButtonBackground(leftBack);


        Drawable rightBack = a.getDrawable(R.styleable.NumAddSubView_rightbutonBackground);
        setSubButtonBackground(rightBack);


//        Drawable textcolor = a.getDrawable(R.styleable.NumAddSubView_textColor);
//        NumText.setTextColor();

        a.recycle();
    }

    private void initView() {
        mView = mInflater.inflate(R.layout.template_numsub_addview, this, true);
        AddButton = (Button) mView.findViewById(R.id.id_add_btn);
        AddButton.setOnClickListener(this);
        SubButton = (Button) mView.findViewById(R.id.id_sub_btn);
        SubButton.setOnClickListener(this);
        NumText = (TextView) mView.findViewById(R.id.id_value_text);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_add_btn:
                Add();
                if (mListener != null) {
                    mListener.AddNum(v,getValue());
                }
                break;

            case R.id.id_sub_btn:
                sub();
                if (mListener != null) {
                    mListener.SubNum(v,getValue());
                }
                break;
        }
    }

    public interface OnAddSubClickListener {

        void AddNum(View view, int value);

        void SubNum(View view,int value);
    }

    public void setAddButtonBackground(Drawable drawable){
        AddButton.setBackground(drawable);
    }

    public void setSubButtonBackground(Drawable drawable){
        SubButton.setBackground(drawable);
    }
}
